<?php
class dbC{
    private $link;
    private $conn;
    private $data;
    private $sql;
    private $affectedRows;
    private $error;
    private $errormsg;
    public function __construct($data) {
        $this->data = $data;
        $this->connect(); 
    }
    public function connect(){
                
       $this->conn = mysqli_init();
        if(!$this->conn){ die("Conection Failed"); }
        if (!mysqli_options($this->conn, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
            die('Setting MYSQLI_INIT_COMMAND failed');
        }
        if (!mysqli_options($this->conn, MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
            die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
        }
        $this->link = mysqli_real_connect($this->conn, $this->data["hostname"], $this->data["username"], $this->data["password"], $this->data["database"]);
        if (!$this->link) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
    public function disconnect(){
        if(!mysqli_close($this->conn)) {
          $this->errormsg = "Error number: " . mysqli_errno($this->conn) . "  --  " . mysqli_error($this->conn);
          die($this->error);
        }
    }
    public function query($sql){
	try{
            $this->sql = $sql;
            if (mysqli_connect_errno()){
                printf("Connect failed: %s\n", mysqli_connect_error());
		exit();
            }
            $result = mysqli_query($this->conn,$this->sql,MYSQLI_USE_RESULT);
            if (mysqli_errno($this->conn)){
                $this->error = true;
                $this->errormsg = "Error number: " . mysqli_errno($this->conn) . "  --  " . mysqli_error($this->conn);
            }
            $rows = array();$cont = 0;
            while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
                  foreach ($row as $key => $value){
                           $rows[$cont][$key] = utf8_encode($value);
                  }$cont++;
            }
            $this->affectedRows = count($rows);
            mysqli_free_result($result);
            return array("status" => true, "total" => $this->affectedRows ,"rows" => $rows);
	   }catch(Exception $e){
                return array("status" => false, "msg" => $e->getMessage());
	   }
   }
   public function execute($sql, $commit = false){
       try{
            if(empty($sql)){
                die("Empty SQL");
            }
            $this->sql = $sql;
            if (!$this->conn) {
                die($this->errormsg);
            }
            if (!mysqli_autocommit($this->conn, $commit)){
                die(mysqli_error($this->link));
            }
            if (!mysqli_query($this->conn, $this->sql)){
                die(mysqli_error($this->link));
            }
            $affected = mysqli_affected_rows($this->conn);
            switch ($affected){
                case -1: $msg = "Consulta invalida: " . $sql;break;
                case 0:  $msg = "No se afecto ningun registro";break;
                default: $msg = "Se agregaron correctamente: " . $affected;break;
            }
	    $idinsert = mysqli_insert_id($this->conn);
            $error = mysqli_error($this->conn);
            if ($error){
                throw new Exception($error);
            }
            return array("status" => true, "msg" => $msg, "rows" => $affected);
	}
        catch(Exception $e){
            return array('status'=>false, "msg" => $e->getMessage());
        }
    }
    public function commit(){
        mysqli_commit($this->conn);
    }

    public function rollback(){
        mysqli_rollback($this->conn);
    }
}
?>